clear all
use YOUR_DIRECTORY\original_nsee_data.dta

append using f2010 f2012 f2013 f2014  s2010 s2012 s2013 s2014 s2015 f2008_f2015_formerge s2011 f2011, force

*Some versions of survey code states with full name or misspelled name. Correct these.
replace demog_state="AL" if demog_state=="ALABAMA"
replace demog_state="AK" if demog_state=="ALASKA"
replace demog_state="AZ" if demog_state=="ARIZONA"
replace demog_state="AZ" if demog_state=="ARIZONIA"
replace demog_state="AR" if demog_state=="ARKANSAS"
replace demog_state="AZ" if demog_state=="Az"
replace demog_state="CA" if demog_state=="CALFORNIA"
replace demog_state="CA" if demog_state=="CALIFORNIA"
replace demog_state="CO" if demog_state=="COLORADO"
replace demog_state="CT" if demog_state=="CONNECTICUT"
replace demog_state="CT" if demog_state=="CONNECTICUTT"
replace demog_state="DC" if demog_state=="D.C."
replace demog_state="DE" if demog_state=="DELAWARE"
replace demog_state="FL" if demog_state=="FLORDIA"
replace demog_state="FL" if demog_state=="FLORIDA"
replace demog_state="GA" if demog_state=="GEORGIA"
replace demog_state="HI" if demog_state=="HAWAII"
replace demog_state="ID" if demog_state=="IDAHO"
replace demog_state="IL" if demog_state=="ILLINIOS"
replace demog_state="IL" if demog_state=="ILLINOIS"
replace demog_state="IL" if demog_state=="ILLNOIS"
replace demog_state="IN" if demog_state=="INDIANA"
replace demog_state="IA" if demog_state=="IO"
replace demog_state="IA" if demog_state=="IOWA"
replace demog_state="KS" if demog_state=="KANASAS"
replace demog_state="KS" if demog_state=="KANSAS"
replace demog_state="KY" if demog_state=="KENTUCKY"
replace demog_state="LA" if demog_state=="LOUISANNA"
replace demog_state="LA" if demog_state=="LOUISIANA"
replace demog_state="LA" if demog_state=="LOUSIANA"
replace demog_state="ME" if demog_state=="MAINE"
replace demog_state="MD" if demog_state=="MARYLAND"
replace demog_state="MA" if demog_state=="MASSACHUSETTES"
replace demog_state="MA" if demog_state=="MASSACHUSETTS"
replace demog_state="MI" if demog_state=="MICHIGAN"
replace demog_state="MN" if demog_state=="MINESOTA"
replace demog_state="MN" if demog_state=="MINNESOTA"
replace demog_state="" if demog_state=="MISSING"
replace demog_state="MS" if demog_state=="MISSISSIPPI"
replace demog_state="MO" if demog_state=="MISSOURI"
replace demog_state="MT" if demog_state=="MONTANA"
replace demog_state="NE" if demog_state=="NEBRASKA"
replace demog_state="NV" if demog_state=="NEVADA"
replace demog_state="NH" if demog_state=="NEW HAMPSHIRE"
replace demog_state="NJ" if demog_state=="NEW JERSEY"
replace demog_state="NJ" if demog_state=="NEW JERSRY"
replace demog_state="NM" if demog_state=="NEW MEXICO"
replace demog_state="NY" if demog_state=="NEW Y0RK"
replace demog_state="NY" if demog_state=="NEW YOK"
replace demog_state="NY" if demog_state=="NEW YORK"
replace demog_state="NC" if demog_state=="NORTH  CAROLINA"
replace demog_state="NC" if demog_state=="NORTH CAROLINA"
replace demog_state="ND" if demog_state=="NORTH DAKOTA"
replace demog_state="OH" if demog_state=="OHIO"
replace demog_state="OK" if demog_state=="OKLAHOMA"
replace demog_state="OR" if demog_state=="OREGAN"
replace demog_state="OR" if demog_state=="OREGON"
replace demog_state="OR" if demog_state=="ORGEGON"
replace demog_state="PA" if demog_state=="PENNSYLVANIA"
replace demog_state="" if demog_state=="REFUSED"
replace demog_state="RI" if demog_state=="RHODE ISLAND"
replace demog_state="RI" if demog_state=="RHODE iSLAND"
replace demog_state="SC" if demog_state=="SOUTH CARLIONA"
replace demog_state="SC" if demog_state=="SOUTH CAROLINA"
replace demog_state="SD" if demog_state=="SOUTH DAKOTA"
replace demog_state="SD" if demog_state=="SOUTY DAKOTA"
replace demog_state="TN" if demog_state=="TENNESSEE"
replace demog_state="TX" if demog_state=="TEXAS"
replace demog_state="UT" if demog_state=="UTAH"
replace demog_state="VT" if demog_state=="VERMONT"
replace demog_state="VA" if demog_state=="VIRGINA"
replace demog_state="VA" if demog_state=="VIRGINIA"
replace demog_state="WA" if demog_state=="WASHINGTON"
replace demog_state="DC" if demog_state=="WASHINGTON D.C"
replace demog_state="DC" if demog_state=="WASHINGTON D.C."
replace demog_state="DC" if demog_state=="WASHINGTON DC"
replace demog_state="WV" if demog_state=="WEST VIRGINIA"
replace demog_state="WI" if demog_state=="WISCONSIN"
replace demog_state="WY" if demog_state=="WYOMING"
replace demog_state="KS" if demog_state=="kANSAS"
replace demog_state="NY" if demog_state=="ny"
replace demog_state="CT" if demog_state=="CONNETICUT"
replace demog_state="KY" if demog_state=="KENTTUCKY"
replace demog_state="KY" if demog_state=="KENYUCKY"
replace demog_state="MO" if demog_state=="MISSOURRI"
replace demog_state="NV" if demog_state=="NEVEDA"
replace demog_state="NJ" if demog_state=="NEW JERSY"
replace demog_state="PA" if demog_state=="PENNSLVANIA"
replace demog_state="PA" if demog_state=="PENNSYVANIA"
replace demog_state="WI" if demog_state=="WISCONCIN"
replace demog_state="WI" if demog_state=="WISCONSON"
drop if demog_state=="MEXICO"
drop if demog_state=="CANADA"

*keep all the variables possibly used in analysis
keep believer_* believer_factor_* demog_* gw_belief gw_noevidence gw_scioverstate gw_nattrend gw_mediaoverstate gw_adapt gw_concern nonbeliever_* willing2pay_* wave year Month Day Year weight 

*create survey save ids
encode wave, gen(swave)

*several versions of survey calls age demog_age_open_coded instead of demog_age_list, but it is actually same coding, so I combine
replace demog_age_list=demog_age_open_coded if demog_age_list==.

*clean up missing values
replace gw_belief=. if gw_belief==98
replace gw_belief=. if gw_belief==99
replace gw_belief=. if gw_belief==.r
replace believer_cause=. if believer_cause==98
replace believer_cause=. if believer_cause==99
replace believer_cause=. if believer_cause==.r
replace believer_confidence=. if believer_confidence==98
replace believer_confidence=. if believer_confidence==.r
replace demog_relig=. if demog_relig==98
replace demog_relig=. if demog_relig==.r
replace demog_race=. if demog_race==98
replace demog_race=. if demog_race==.r
replace demog_edu=. if demog_edu==98
replace demog_edu=. if demog_edu==.r
replace demog_news=. if demog_news==98
replace demog_news=. if demog_news==.r
replace demog_polp=. if demog_polp==98
replace demog_polp=. if demog_polp==.r
replace demog_age_list=. if demog_age_list==98
replace demog_age_list=. if demog_age_list==5
replace demog_age_list=. if demog_age_list==.r
replace demog_income=. if demog_income==98
replace demog_income=. if demog_income==.r
replace demog_gender=. if demog_gender==98
replace demog_gender=. if demog_gender==.r

*make indicator for gender. 1=male, 0=female
replace demog_gender=0 if demog_gender==2
*make indicator for gw believ. 1=belives, 0=does not believe
replace gw_belief=0 if gw_belief==2

*create consistent coding for race/ethnicity categories across survey waves
replace demog_race=9 if demog_race==3
replace demog_race=9 if demog_race==4

*create consistent coding for religion categories across survey waves
replace demog_relig=6 if demog_relig==4
replace demog_relig=6 if demog_relig==5

*clean up missing religion values
replace demog_relig_evang=. if demog_relig_evang==98
replace demog_relig_evang=. if demog_relig_evang==.n
replace demog_relig_evang=. if demog_relig==.r

*clean up missing political values
replace demog_teaparty=. if demog_teaparty==98
replace demog_polviews=. if demog_polviews==98
replace demog_polviews=. if demog_polviews==.r

*clean up missing age values
replace demog_age_list=. if demog_age_list==5
replace demog_age_list=. if demog_age_list==98

*indicator for older individual
gen older=demog_age_list
replace older=0 if demog_age_list!=.
replace older=1 if demog_age_list>=3 & demog_age_list!=.

*indicator for college educated
gen college=demog_edu
replace college=0 if demog_edu!=.
replace college=1 if demog_edu>=4 & demog_edu!=.

*reformat some variables
recast double gw_belief
recast double demog_polviews
*rename several variables for merge
rename year year_match
rename demog_state state
*create state ids
encode state, gen(stateid)

*indicator for white race
gen white=demog_race
replace white=0 if demog_race>1 & demog_race!=.

*clean up political party. keep if democrat, republican, or independent
replace demog_polp=. if demog_polp==3
replace demog_polp=. if demog_polp==5

*just a duplicate variable with shorter name
gen ppa=demog_polp

*indicator for republican
gen republican=ppa
replace republican=1 if ppa==2
replace republican=0 if ppa==1 | ppa==4

*indicator for democrat
gen democrat=demog_polp
replace democrat=0 if demog_polp!=1

*indicator for independent
gen independent=0 if ppa!=.
replace independent=1 if ppa==4

*race/ethnicity fixed effects
tab demog_race, gen (rfe)

*year fixed effects
tab year_match, gen (yfe)

*survey wave fixed effects
tab swave, gen (wavefe)

*political affiliation fixed effects
tab ppa,gen (pfe)

*generate dichotomous variable for human caused global warming
replace believer_cause=. if believer_cause==.n
gen believer_human=gw_belief
replace believer_human=0 if believer_cause==2

*indicator for conservative
gen conservative=0
replace conservative=1 if demog_polviews==1 | demog_polviews==2
replace conservative=. if demog_polviews==.

*indicator for liberal
gen liberal=0
replace liberal=1 if demog_polviews==4 | demog_polviews==5
replace liberal=. if demog_polviews==.


*merge in gubernatorial election data
merge m:1 state year_match using allstates_formerge_final

keep if _merge==3


*Florida governor independent 2010
replace m_percent=. if state=="FL" & year_match==2010
*replace m_percent=. if state=="FL" & year_match==2009

*Arizona Secretary of state (Rep) took over for Dem gov 2009-2010, 
replace m_percent=. if state=="AZ" & year_match==2009
replace m_percent=. if state=="AZ" & year_match==2010

*sample selection criteria
gen sample=0
replace sample=1 if  demog_gender!=. & gw_belief!=. & m_percent!=. & demog_polp!=. & demog_race!=.


keep if sample==1

*create variables for parametric RD specifications
gen r_win=0
replace r_win=1 if m_percent>0
gen int_rd=r_win*m_percent

gen m_percent2=m_percent^2
gen int_rd2=int_rd^2

gen m_percent3=m_percent^3
gen int_rd3=int_rd^3

gen m_percent4=m_percent^4
gen int_rd4=int_rd^4

gen m_percent5=m_percent^5
gen int_rd5=int_rd^5

gen p2Xm=pfe2*m_percent
gen p3Xm=pfe3*m_percent
gen p2Xint=pfe2*int_rd
gen p3Xint=pfe3*int_rd
gen p2Xwin=pfe2*r_win
gen p3Xwin=pfe3*r_win

gen p2Xm2=pfe2*m_percent2
gen p3Xm2=pfe3*m_percent2
gen p2Xint2=pfe2*int_rd2
gen p3Xint2=pfe3*int_rd2

gen p2Xm3=pfe2*m_percent3
gen p3Xm3=pfe3*m_percent3
gen p2Xint3=pfe2*int_rd3
gen p3Xint3=pfe3*int_rd3

gen p2Xm4=pfe2*m_percent4
gen p3Xm4=pfe3*m_percent4
gen p2Xint4=pfe2*int_rd4
gen p3Xint4=pfe3*int_rd4

gen p2Xm5=pfe2*m_percent5
gen p3Xm5=pfe3*m_percent5
gen p2Xint5=pfe2*int_rd5
gen p3Xint5=pfe3*int_rd5




*Variables for placebo test where I use future election outcome and current GW beliefs
gen r_win_p1=.
replace r_win_p1=0 if m_percent_p1<0
replace r_win_p1=1 if m_percent_p1>0
gen int_rd_p1=r_win_p1*m_percent_p1

gen p2Xmp1=pfe2*m_percent_p1
gen p3Xmp1=pfe3*m_percent_p1
gen p2Xintp1=pfe2*int_rd_p1
gen p3Xintp1=pfe3*int_rd_p1
gen p2Xwinp1=pfe2*r_win_p1
gen p3Xwinp1=pfe3*r_win_p1

*Variables for looking at covariate balance
gen above6=0 if m_percent>=-6 & m_percent<0
replace above6=1 if m_percent>0 & m_percent<=6

gen above3=0 if m_percent>=-3 & m_percent<0
replace above3=1 if m_percent>0 & m_percent<=3

*state fixed effects
tab stateid, gen(state_fe)



save politicalclimate_foranalysis_final, replace
